package code.example.hystrix.test.impl;

import code.example.tool.LogFactory;
import com.netflix.hystrix.HystrixCommand;
import rx.Observable;
import rx.Subscriber;

import java.util.concurrent.TimeUnit;

public class HystrixCommandVisitHttpAsyncCall {

    public void asyncCall(HystrixCommand<String> hystrixCommand){
        Observable<String> observable =  hystrixCommand.toObservable();
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                LogFactory.getLog().info(Thread.currentThread().getName()+": run finish");
            }

            @Override
            public void onError(Throwable e) {
                LogFactory.getLog().info(Thread.currentThread().getName()+": run error");
                LogFactory.getLog().error(e);
            }

            @Override
            public void onNext(String s) {
                LogFactory.getLog().info(Thread.currentThread().getName()+": run on next:"+s);
            }
        });
    }

    public static void main(String [] args){
        HystrixCommandVisitHttp hystrixCommandVisitHttp = new HystrixCommandVisitHttp();
        new HystrixCommandVisitHttpAsyncCall().asyncCall(hystrixCommandVisitHttp);
        try{
            TimeUnit.MINUTES.sleep(1);
        }catch (Exception e){};
    }
}
